/**
 * Created by Jaye on 2017/12/22.
 */
// 提交表单
function delete_info(id) {
    if (!id) {
        alert('Error！');
        return false;
    }
    // var form_data = new Array();

    $.ajax(
        {
            url: "action/user_action.php",
            data: {"id": id, "act": "del"},
            type: "post",
            beforeSend: function () {
                $("#tip").html("<span style='color:blue'>正在处理...</span>");
                return true;
            },
            success: function (data) {
                if (data > 0) {
                    alert('操作成功');
                    $("#tip").html("<span style='color:blueviolet'>恭喜，删除成功！</span>");

                    // document.location.href='world_system_notice.php'
                    location.reload();
                }
                else {
                    $("#tip").html("<span style='color:red'>失败，请重试</span>");
                    alert('操作失败');
                }
            },
            error: function () {
                alert('请求出错');
            },
            complete: function () {
                // $('#tips').hide();
            }
        });

    return false;
}

// 编辑表单
function get_edit_info(user_id) {
    if (!user_id) {
        alert('Error！');
        return false;
    }
    // var form_data = new Array();

    $.ajax(
        {
            url: "action/user_action.php",
            data: {"user_id": user_id, "act": "get"},
            type: "post",
            beforeSend: function () {
                // $("#tip").html("<span style='color:blue'>正在处理...</span>");
                return true;
            },
            success: function (data) {
                if (data) {

                    // 解析json数据
                    var data = data;

                    var data_obj = eval("(" + data + ")");

                    // 赋值
                    $("#user_id").val(data_obj.user_id);

                    $("#name").val(data_obj.name);
                    $("#address").val(data_obj.address);
                    $("#remark").val(data_obj.remark);
                    $("#act").val("edit");

                    // 将input元素设置为readonly
                    $('#user_id').attr("readonly", "readonly")
                    // location.reload();
                }
                else {
                    $("#tip").html("<span style='color:red'>失败，请重试</span>");
                    //  alert('操作失败');
                }
            },
            error: function () {
                alert('请求出错');
            },
            complete: function () {
                // $('#tips').hide();
            }
        });

    return false;
}
function refresh() {
    location.reload();
}
// 提交表单
function check_form() {
    var accesspoint_username = $.trim($('#accesspoint_username').val());
    var accesspoint_nickname = $.trim($('#accesspoint_nickname').val());
    var form_data = $('#form_data').serialize();
    $.ajax(
        {
            url: "accesspoint/create",
            data: form_data,
            type: "post",
            dataType: "json",
            beforeSend: function () {
                $("#tip").html("<span style='color:blue'>正在处理...</span>");
                return true;
            },
            success: function (data) {
                if (data.code == 1000) {
                    if (data.data.executeResult) {
                        swal({
                            title: "新增成功！",
                            type: "success"
                        });
                        location.reload();
                        return;
                    }
                }
                $("#tip").html("<span style='color:red'>失败，请重试</span>");
                swal({
                    title: "操作失败！",
                    type: "error"
                });
            },
            error: function () {
                swal({
                    title: "请求出错！",
                    type: "error"
                });
            },
            complete: function () {
                $('#acting_tips').hide();
            }
        });

    // return false;
}

function enableAccessPoint(accessPointId) {
    $.ajax(
        {
            url: "accesspoint/enable",
            data: {"accessPointId": accessPointId},
            type: "post",
            dataType: "json",
            success: function (data) {
                if (data.code == 1000) {
                    if (data.data.executeResult) {
                        swal({
                            title: "启用成功",
                            text: "接入点ID为:" + accessPointId,
                            type: "success",
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "确认",
                            closeOnConfirm: false
                        }, function () {
                            location.reload();
                        });
                        return;
                    }
                }
                swal({
                    title: "操作失败",
                    type: "error",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
            },
            error: function () {
                swal({
                    title: "请求出错",
                    type: "error",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
            }
        });
}

function disableAccessPoint(accessPointId) {
    $.ajax(
        {
            url: "accesspoint/disable",
            data: {"accessPointId": accessPointId},
            type: "post",
            dataType: "json",
            success: function (data) {
                if (data.code == 1000) {
                    if (data.data.executeResult) {
                        swal({
                            title: "禁用成功",
                            text: "接入点ID为:" + accessPointId,
                            type: "success",
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "确认",
                            closeOnConfirm: false
                        }, function () {
                            location.reload();
                        });
                        // setTimeout(1000);

                        return;
                    }
                }
                swal({
                    title: "操作失败",
                    type: "error",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
            },
            error: function () {
                swal({
                    title: "请求出错",
                    type: "error",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
            }
        });
}

$(function () {
    $("#exampleTableFromData").bootstrapTable({
        url: "accesspoint/list",
        method: "post",
        height: "500",
        iconSize: "outline",
        search: true,
        showRefresh: true,
        pagination: true,
        showColumns: !0,
        singleSeclect: true,
        columns: [
            {radio: true},
            {
                title: "接入点Id",
                field: "accessPointId",
                align: "center"
            }, {
                title: "接入点名称",
                field: "accessPointUserName",
                align: "center"
            }, {
                title: "接入点昵称",
                field: "accessPointNickName",
                align: "center"
            }, {
                title: "启用状态",
                field: "enableState",
                align: "center",
                formatter: function (value) {
                    if (value == 1) {
                        return "<span type='text' class='btn btn-primary btn-sm'>已启用</span>";
                    } else {
                        return "<span type='text' class='btn btn-danger btn-sm'>已禁用</span>";
                    }
                }
            }],
        icons: {
            refresh: "glyphicon-repeat",
            toggle: "glyphicon-list-alt",
            columns: "glyphicon-list"
        }
    });
    $('#addAccessPointModal').on('hide.bs.modal', function () {
        // 关闭时清空edit状态为add
        $("#act").val("add");
        location.reload();
    });
    $('#refreshbutton').click(function () {
        location.reload();
    });
    $('#enableButton').click(function () {
        var selectContent = $("#exampleTableFromData").bootstrapTable('getSelections')[0];
        if (typeof(selectContent) == 'undefined') {
            tip_noselect();
            return false;
        } else {
            // console.info(selectContent);
            if (selectContent.enableState == 1) {
                swal({
                    title: "请重新选择",
                    text: "该接入点已经是启用状态",
                    type: "warning",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
                return;
            } else {
                enableAccessPoint(selectContent.accessPointId);
            }
        }
    });
    $('#disableButton').click(function () {
        var selectContent = $("#exampleTableFromData").bootstrapTable('getSelections')[0];
        if (typeof(selectContent) == 'undefined') {
            // toastr.warning('请选择一列数据!');
            tip_noselect();
            // $("#no_record").alert();
            return false;
        } else {
            // console.info(selectContent);
            if (selectContent.enableState == 0) {
                swal({
                    title: "请重新选择",
                    text: "该接入点已经是禁用状态",
                    type: "warning",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
                return;
            } else {
                disableAccessPoint(selectContent.accessPointId);
            }
        }
    });
});
function tip_noselect() {
    swal({
        title: "请选择一列数据",
        type: "warning",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        closeOnConfirm: false
    });
}